<template>
  <div>
    <!--列表-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.deptName" placeholder="输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.deptNo" placeholder="输入用户姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
              element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="index" width="60"/>
      <el-table-column sortable prop="username" label="用户名" width="150"/>
      <el-table-column sortable prop="password" label="密码" width="150"/>
      <el-table-column sortable prop="email" label="邮箱" width="150"/>
      <el-table-column sortable prop="last_login" label="上一次登录时间" width="150"/>
      <el-table-column sortable prop="is_superuser" label="超管" width="150"/>
      <el-table-column sortable prop="first_name" label="名" width="150"/>
      <el-table-column sortable prop="last_name" label="姓" width="150"/>
      <el-table-column sortable prop="is_staff" label="是否可登录管理站点" width="150"/>
      <el-table-column sortable prop="is_active" label="是否活跃" width="150"/>
      <el-table-column sortable prop="date_joined" label="date_joined" width="150"/>
      <el-table-column sortable prop="groups" label="用户权限组" width="150"/>
      <el-table-column sortable prop="user_permissions" label="用户的特定权限" width="150"/>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="用户名" prop="username">
          <el-input size="small" v-model="editForm.username" auto-complete="off" placeholder="请输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="small" v-model="editForm.password" auto-complete="off" placeholder="请输入学生入学年份"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input size="small" v-model="editForm.email" auto-complete="off" placeholder="请输入学生学历"></el-input>
        </el-form-item>
        <el-form-item label="上一次登录时间" prop="last_login">
          <el-input size="small" v-model="editForm.last_login" auto-complete="off" placeholder="请输入学生是否在校"></el-input>
        </el-form-item>
        <el-form-item label="超管" prop="is_superuser">
          <el-input size="small" v-model="editForm.is_superuser" auto-complete="off" placeholder="请输入学生是否毕业"></el-input>
        </el-form-item>
        <el-form-item label="名" prop="first_name">
          <el-input size="small" v-model="editForm.first_name" auto-complete="off" placeholder="请输入学生毕业去处"></el-input>
        </el-form-item>
        <el-form-item label="姓" prop="last_name">
          <el-input size="small" v-model="editForm.last_name" auto-complete="off" placeholder="请输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item label="是否可登录管理站点" prop="is_staff">
          <el-input size="small" v-model="editForm.is_staff" auto-complete="off" placeholder="请输入学生入学年份"></el-input>
        </el-form-item>
        <el-form-item label="是否活跃" prop="is_active">
          <el-input size="small" v-model="editForm.is_active" auto-complete="off" placeholder="请输入学生学历"></el-input>
        </el-form-item>
        <el-form-item label="date_joined" prop="date_joined">
          <el-input size="small" v-model="editForm.date_joined" auto-complete="off" placeholder="请输入学生是否在校"></el-input>
        </el-form-item>
        <el-form-item label="用户权限组" prop="groups">
          <el-input size="small" v-model="editForm.groups" auto-complete="off" placeholder="请输入学生是否毕业"></el-input>
        </el-form-item>
        <el-form-item label="用户的特定权限" prop="user_permissions">
          <el-input size="small" v-model="editForm.user_permissions" auto-complete="off" placeholder="请输入学生毕业去处"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { deptList, deptSave, deptDelete } from '../../api/userMG'
import Pagination from '../../components/Pagination'

export default {
  data () {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        username : '',
        password : '',
        last_login: '',
        is_superuser: '',
        first_name: '',
        last_name: '',
        email : '',
        is_staff: '',
        is_active: '',
        date_joined: '',
        groups: '',
        user_permissions: '',
        token: localStorage.getItem('logintoken'),
      },
      // rules表单验证
      rules: {
        name: [{ required: true, message: '请输入学生姓名', trigger: 'blur' }],
        join_year: [{ required: true, message: '请输入学生入学年份', trigger: 'blur' }],
        is_graduated: [{ required: true, message: '请输入学生是否毕业', trigger: 'blur' }],
        is_school: [{ required: true, message: '请输入学生是否在校', trigger: 'blur' }],
      },
      formInline: {
        page: 1,
        limit: 10,
        varLable: '',
        varName: '',
        token: localStorage.getItem('logintoken'),
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken'),
      },
      listData: [
        {
          username : 'GDGCC',
          password : '11111',
          last_login: '2021.7.15',
          is_superuser: '是',
          first_name: 'CAAAA',
          last_name: 'CCHHH',
          email : 's11nn@111.com',
          is_staff: '是',
          is_active: '是',
          date_joined: '-',
          groups: 'high',
          user_permissions: 'yes',
        },
        {
          username : 'JAAVVA',
          password : '3321321',
          last_login: '2021.7.12',
          is_superuser: '是',
          first_name: 'JA',
          last_name: 'SUN',
          email : 'sunnnn@111.com',
          is_staff: '是',
          is_active: '是',
          date_joined: '-',
          groups: 'low',
          user_permissions: 'no',
        },
      ], 
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    }
  },

  /**
   * 数据发生改变
   */
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 创建完毕
   */
  created () {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取学生列表
    getdata (parameter) {
      this.loading = true
      this.loading = false
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      // deptList(parameter)
      //   .then(res => {
      //     this.loading = false
      //     if (res.success == false) {
      //       this.$message({
      //         type: 'info',
      //         message: res.msg
      //       })
      //     } else {
      //       this.listData = res.data
      //       // 分页赋值
      //       this.pageparm.currentPage = this.formInline.page
      //       this.pageparm.pageSize = this.formInline.limit
      //       this.pageparm.total = res.count
      //     }
      //   })
      //   .catch(err => {
      //     this.loading = false
      //     this.$message.error('菜单加载失败，请稍后再试！')
      //   })
    },
    // 分页插件事件
    callFather (parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search () {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      if (row !== undefined && row !== 'undefined') {
        console.log('哈哈哈哈哈哈哈哈')
        this.title = '修改'
        this.editForm.username = row.username
        this.editForm.password = row.password
        this.editForm.last_login = row.last_login
        this.editForm.is_superuser = row.is_superuser
        this.editForm.first_name = row.first_name
        this.editForm.last_name = row.last_name
        this.editForm.email = row.email
        this.editForm.is_staff = row.is_staff
        this.editForm.is_active = row.is_active
        this.editForm.date_joined = row.date_joined
        this.editForm.groups = row.groups
        this.editForm.user_permissions = row.user_permissions
      } else {
        this.title = '添加'
        this.editForm.username = ''
        this.editForm.password = ''
        this.editForm.last_login = ''
        this.editForm.is_superuser = ''
        this.editForm.first_name = ''
        this.editForm.last_name = ''
        this.editForm.email = ''
        this.editForm.is_staff = ''
        this.editForm.is_active = ''
        this.editForm.date_joined = ''
        this.editForm.groups = ''
        this.editForm.user_permissions = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm (editData) {
      this.$message({
        type: 'success',
        message: '添加/修改操作成功',
      })
      // this.$refs[editData].validate(valid => {
      //     if (valid) {
      //       deptSave(this.editForm).then(res => {
      //         this.editFormVisible = false
      //         this.loading = false
      //         if (res.success) {
      //           this.getdata(this.formInline)
      //           this.$message({
      //             type: 'success',
      //             message: '公司保存成功！',
      //           })
      //         } else {
      //           this.$message({
      //             type: 'info',
      //             message: res.msg,
      //           })
      //         }
      //       }).catch(err => {
      //         this.editFormVisible = false
      //         this.loading = false
      //         this.$message.error('公司保存失败，请稍后再试！')
      //       })
      //     } else {
      //       return false
      //     }
      //   },
      // )
    },
// 删除公司
    deleteUser (index, row) {
      this.$message({
        type: 'success',
        message: '删除操作成功',
      })
      // this.$confirm('确定要删除吗?', '信息', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning',
      // }).then(() => {
      //   deptDelete(row.deptId).then(res => {
      //     if (res.success) {
      //       this.$message({
      //         type: 'success',
      //         message: '公司已删除!',
      //       })
      //       this.getdata(this.formInline)
      //     } else {
      //       this.$message({
      //         type: 'info',
      //         message: res.msg,
      //       })
      //     }
      //   }).catch(err => {
      //     this.loading = false
      //     this.$message.error('公司删除失败，请稍后再试！')
      //   })
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消删除',
      //   })
      // })
    }
    ,
// 关闭编辑、增加弹出框
    closeDialog () {
      this.editFormVisible = false
    }
    ,
  },
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>